package com.kmx.demo.datagen;

import java.text.DecimalFormat;
import java.text.Format;
import java.util.ArrayList;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kmx.demo.utils.K2FileUtils;
import com.kmx.demo.utils.UUID;

public class GenMetaData {

	public static void main(String[] args) {
		String uuid = UUID.get();
		genFixedFieldGroup("data/gw", uuid, 1);
		genAssets("data/gw", uuid, 1, 3);
	}

	public static JSONObject genField(String id, String type, boolean isIdField) {

		JSONObject field = new JSONObject();
		field.put("id", id);
		field.put("valueType", type);
		field.put("isIdField", isIdField);
		return field;
	}

	public static JSONArray genTurbineFields() {

		JSONArray fields = new JSONArray();
		fields.add(genField("turbineId", "STRING", true));
		fields.add(genField("windSpeed", "FLOAT", false));
		fields.add(genField("powerActive", "FLOAT", false));
		fields.add(genField("generatorSpeed", "FLOAT", false));
		fields.add(genField("pitchAngle", "DOUBLE", false));
		return fields;

	}

	private static String format(int number) {
		Format f = new DecimalFormat("000");
		return f.format(number);
	}

	public static void genFieldGroups(String dir, String uuid, int fgNum) {
		JSONObject fileGroup = new JSONObject();
		for (int i = 0; i < fgNum; i++) {
			fileGroup.put("id", "windfarm_" + uuid + "_" + format(i + 1));
			fileGroup.put("fields", genTurbineFields());
			K2FileUtils.writeContentsToFile(K2FileUtils.filePath(dir) + "fieldGroup",
					fileGroup.toJSONString() + "\n", true);
		}

	}

	public static void genFixedFieldGroup(String dir, String uuid, int fgNum) {

		String field = "[{\"valueType\":\"STRING\",\"isIdField\":true,\"id\":\"turbineId\",\"name\":\"风机编号\"},{\"valueType\":\"FLOAT\",\"isIdField\":false,\"id\":\"windSpeed\",\"name\":\"风速\",\"unit\":\"m/s\",\"intervals\":[repFreq]},{\"valueType\":\"FLOAT\",\"isIdField\":false,\"id\":\"powerActive\",\"name\":\"功率\",\"unit\":\"kW\",\"intervals\":[repFreq]},{\"valueType\":\"FLOAT\",\"isIdField\":false,\"id\":\"generatorSpeed\",\"name\":\"发电机转速\",\"unit\":\"rpm\",\"intervals\":[repFreq]},{\"valueType\":\"DOUBLE\",\"isIdField\":false,\"id\":\"pitchAngle\",\"name\":\"浆距角\",\"unit\":\"°\",\"intervals\":[repFreq]}]";
		JSONObject fileGroup = new JSONObject();
		for (int i = 0; i < fgNum; i++) {
			fileGroup.put("id", "windfarm_" + uuid + "_" + format(i + 1));
			fileGroup.put("fields", new ArrayList<>());

			String content = fileGroup.toJSONString().replace("[]", field);
			K2FileUtils.writeContentsToFile(K2FileUtils.filePath(dir) + "fieldGroups", content + "\n", true);
		}
	}
	
	
	public static void genFixedFieldGroup(String dir, String uuid, int fgNum,String freq) {

		String field = "[{\"valueType\":\"STRING\",\"isIdField\":true,\"id\":\"turbineId\",\"name\":\"风机编号\"},{\"valueType\":\"FLOAT\",\"isIdField\":false,\"id\":\"windSpeed\",\"name\":\"风速\",\"unit\":\"m/s\",\"intervals\":[repFreq]},{\"valueType\":\"FLOAT\",\"isIdField\":false,\"id\":\"powerActive\",\"name\":\"功率\",\"unit\":\"kW\",\"intervals\":[repFreq]},{\"valueType\":\"FLOAT\",\"isIdField\":false,\"id\":\"generatorSpeed\",\"name\":\"发电机转速\",\"unit\":\"rpm\",\"intervals\":[repFreq]},{\"valueType\":\"DOUBLE\",\"isIdField\":false,\"id\":\"pitchAngle\",\"name\":\"浆距角\",\"unit\":\"°\",\"intervals\":[repFreq]}]";
		field=field.replace("repFreq", freq+"");
		JSONObject fileGroup = new JSONObject();
		for (int i = 0; i < fgNum; i++) {
			fileGroup.put("id", "windfarm_" + uuid + "_" + format(i + 1));
			fileGroup.put("fields", new ArrayList<>());

			String content = fileGroup.toJSONString().replace("[]", field);
			K2FileUtils.writeContentsToFile(K2FileUtils.filePath(dir) + "fieldGroups", content + "\n", true);
		}
	}
	

	public static ArrayList<String> genFgIdList(String uuid, int fgNum) {
		ArrayList<String> fgIds = new ArrayList<String>();
		for (int i = 0; i < fgNum; i++) {
			fgIds.add("windfarm_" + uuid + "_" + format(i + 1));
		}
		return fgIds;
	}

	public static JSONObject genCompoundId(String idValue) {

		JSONObject compoundId = new JSONObject();
		compoundId.put("turbineId", idValue);
		return compoundId;

	}

	public static void genAssets(String dir, String uuid, int fgNum, int numPerFg) {

		JSONObject asset = new JSONObject();

		for (int x = 0; x < fgNum; x++) {
			String fgId = "windfarm_" + uuid + "_" + format(x + 1);
			for (int y = 0; y < numPerFg; y++) {
				String assetId = "turbine_" + format(x + 1) + "_" + uuid + "_" + format(y + 1);
				asset.put("fieldGroupId", fgId);
				asset.put("compoundId", genCompoundId(assetId));
				K2FileUtils.writeContentsToFile(K2FileUtils.filePath(dir) + "assets",
						asset.toJSONString() + "\n", true);
			}

		}

	}

}
